package co.com.javeriana.aes.oms.login;

import co.com.javeriana.aes.oms.constants.NavRules;
import co.com.javeriana.aes.oms.constants.Permissions;
import co.com.javeriana.aes.oms.util.Util;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Jonathan Fernandez <jonathan.fvargas@gmail.com>
 */
@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean implements Serializable {

    // <editor-fold defaultstate="collapsed" desc="Atributes">
    private static final long serialVersionUID = 1L;
    private String password;
    private String message, uname, userPermission;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Properties">
    public String getUserPermission() {
        return userPermission;
    }

    public void setUserPermission(String userPermission) {
        this.userPermission = userPermission;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

  //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Methods">
    public String loginProject() {
        boolean result = login(uname, password);
        if (result) {
            // get Http Session and store username
            HttpSession session = Util.getSession();
            session.setAttribute("username", uname);
            //TODO : al momento de ir a produccion 
            //cambiar por la linea que va al ldap
            //userPermission = Permissions.FULL_ACCESS.name();
            userPermission = getUserPermission(uname, password);
            return NavRules.GO_TO_HOME.name();
        } else {
            FacesContext context = FacesContext.getCurrentInstance();            
            context.addMessage( null, new FacesMessage("Error", "Nombre de usuario y/o contraseña erróneos") );
        }
        return "";
    }

    private boolean login(String username, String passwd) {
        return co.com.javeriana.aes.oms.ldap.client.LdapClient.validateUser(username, passwd);
        //return username.equals("admin") && passwd.equals("admin");
    }

    private String getUserPermission(String username, String passwd) {
        return co.com.javeriana.aes.oms.ldap.client.LdapClient.getUserPermission(username, passwd);
    }

    public String logout() {
        HttpSession session = Util.getSession();
        session.invalidate();
        return NavRules.GO_TO_LOGIN.name();
    }

    public String goToIndex() {
        return NavRules.GO_TO_HOME.name();
    }
    //</editor-fold>    
}
